package com.org.api.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.org.api.admin.controller.CmsUserRedisController;
import com.org.api.admin.entity.po.*;
import com.org.api.admin.service.CmsUserMobileGraphicNavigationService;
import com.org.api.admin.service.CmsUserRedisService;
import com.org.platform.utils.LocalDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @auther dh
 * @date 2022/3/3 15:48
 */
@Slf4j
@Service
public class CmsUserMobileGraphicNavigationServiceImpl implements CmsUserMobileGraphicNavigationService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CmsUserRedisService cmsUserRedisService;

    @Override
    public CmsUserMobileGraphicNavigationVO getModuleDetailData(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        // 这里通过模块id查询对应未删除的图文导航数据

        String[] areaCodes = {"430050", "420100"};
        String[] userTypes = {"1103", "1102"};

        CmsUserMobileGraphicNavigationVO cmsUserMobileGraphicNavigationVO = new CmsUserMobileGraphicNavigationVO();

        List<CmsUserMobileGraphicNavigationDetailVO> dataList = new ArrayList<>();
        CmsUserMobileGraphicNavigationDetailVO cmsUserMobileGraphicNavigationDetailVO = new CmsUserMobileGraphicNavigationDetailVO();
        cmsUserMobileGraphicNavigationDetailVO.setTitle("图文导航1");
        cmsUserMobileGraphicNavigationDetailVO.setPictureUrl("http://www.baidu.com");
        cmsUserMobileGraphicNavigationDetailVO.setLinkType(1);
        cmsUserMobileGraphicNavigationDetailVO.setLinkUrl("商品详情页");
        cmsUserMobileGraphicNavigationDetailVO.setOrderSort(1);
        dataList.add(cmsUserMobileGraphicNavigationDetailVO);

        cmsUserMobileGraphicNavigationVO.setList(dataList);
        cmsUserMobileGraphicNavigationVO.setAreaCodes(areaCodes);
        cmsUserMobileGraphicNavigationVO.setUserTypes(userTypes);
        return cmsUserMobileGraphicNavigationVO;
    }

    @Override
    public void invokeModuleDetailData(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        String moduleDetailKey = getDetailKey(CmsUserModuleEnum.GraphicNavigation.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());
        String moduleDetailUserKey = getDetailUserKey(CmsUserModuleEnum.GraphicNavigation.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());

        Object object = cmsUserRedisIndexModule.getModuleDetailData();
//        CmsUserMobileGraphicNavigationVO cmsUserMobileGraphicNavigationVO = CmsUserRedisServiceImpl.getObjectMapper().convertValue(object, CmsUserMobileGraphicNavigationVO.class);
        CmsUserMobileGraphicNavigationVO cmsUserMobileGraphicNavigationVO = (CmsUserMobileGraphicNavigationVO)object;
        String[] areaCodes = cmsUserMobileGraphicNavigationVO.getAreaCodes();
        String[] userTypes = cmsUserMobileGraphicNavigationVO.getUserTypes();
        String[] userArray = new String[areaCodes.length + userTypes.length];
        int arrayPos = 0;
        for (String areaCode : areaCodes) {
            userArray[arrayPos] = CmsUserRedisServiceImpl.moduleUserAreaPrefix + areaCode;
            arrayPos++;
        }
        for (String userType : userTypes) {
            userArray[arrayPos] = CmsUserRedisServiceImpl.moduleUserTypePrefix + userType;
            arrayPos++;
        }
        redisTemplate.delete(moduleDetailUserKey);
        Integer isDelete = cmsUserRedisIndexModule.getIsDelete();
        if (null != isDelete && isDelete.equals(1)) {
            redisTemplate.delete(moduleDetailKey);
            return;
        }
        List<CmsUserMobileGraphicNavigationDetailVO> cmsUserMobileGraphicNavigationDetailVOList = cmsUserMobileGraphicNavigationVO.getList();
        CmsUserMobileGraphicNavigationVO cmsMobileGraphicNavigationVO = new CmsUserMobileGraphicNavigationVO();
        cmsMobileGraphicNavigationVO.setIsLongTerm(CmsUserRedisController.isLongTeam);
        cmsMobileGraphicNavigationVO.setShowStartTime(CmsUserRedisController.startTimeString);
        cmsMobileGraphicNavigationVO.setShowEndTime(CmsUserRedisController.endTimeString);
        cmsMobileGraphicNavigationVO.setList(cmsUserMobileGraphicNavigationDetailVOList);
        redisTemplate.opsForValue().set(moduleDetailKey, cmsMobileGraphicNavigationVO);
        stringRedisTemplate.opsForSet().add(moduleDetailUserKey, userArray);
        long expireTime = cmsUserRedisService.getModuleDetailExpireTime(cmsMobileGraphicNavigationVO.getIsLongTerm(), cmsMobileGraphicNavigationVO.getShowEndTime());
        if (expireTime > 0) {
            redisTemplate.expire(moduleDetailKey, expireTime, TimeUnit.MILLISECONDS);
            redisTemplate.expire(moduleDetailUserKey, expireTime, TimeUnit.MILLISECONDS);
        }
    }

    @Override
    public CmsUserMobileGraphicNavigationVO getRedisDataList(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        String moduleDetailKey = getDetailKey(CmsUserModuleEnum.GraphicNavigation.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());
        String moduleDetailUserKey = getDetailUserKey(CmsUserModuleEnum.GraphicNavigation.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());

        boolean areaExists = false;
        for (String areaCode : CmsUserRedisController.currentAreaCodes) {
            String areaCodeKey = CmsUserRedisServiceImpl.moduleUserAreaPrefix + areaCode;
            boolean areaSetExists = stringRedisTemplate.opsForSet().isMember(moduleDetailUserKey, areaCodeKey);
            if (areaSetExists) {
                areaExists = true;
                break;
            }
        }
        boolean userTypeExists = false;
        for (String userType : CmsUserRedisController.currentUserTypes) {
            String userTypeKey = CmsUserRedisServiceImpl.moduleUserTypePrefix + userType;
            boolean userTypeSetExists = stringRedisTemplate.opsForSet().isMember(moduleDetailUserKey, userTypeKey);
            if (userTypeSetExists) {
                userTypeExists = true;
                break;
            }
        }
        CmsUserMobileGraphicNavigationVO cmsMobileGraphicNavigationVO = new CmsUserMobileGraphicNavigationVO();
        if (areaExists && userTypeExists) {
            Object object = redisTemplate.opsForValue().get(moduleDetailKey);
            cmsMobileGraphicNavigationVO = CmsUserRedisServiceImpl.getObjectMapper().convertValue(object, CmsUserMobileGraphicNavigationVO.class);
            if (null != cmsMobileGraphicNavigationVO && null != cmsMobileGraphicNavigationVO.getIsLongTerm()
                    && cmsMobileGraphicNavigationVO.getIsLongTerm().equals(1)) {
                return cmsMobileGraphicNavigationVO;
            }
            String showStartTime = cmsMobileGraphicNavigationVO.getShowStartTime();
            String showEndTime = cmsMobileGraphicNavigationVO.getShowEndTime();
            LocalDateTime startTime = LocalDateUtil.localDateTimeParse(showStartTime, LocalDateUtil.FORMAT_PATTERN1);
            LocalDateTime endTime = LocalDateUtil.localDateTimeParse(showEndTime, LocalDateUtil.FORMAT_PATTERN1);
            if (startTime.compareTo(LocalDateTime.now()) > 0) {
                cmsMobileGraphicNavigationVO.setList(null); // 未开始
            }
            if (endTime.compareTo(LocalDateTime.now()) <= 0) {
                cmsMobileGraphicNavigationVO.setList(null);// 已结束
            }
        }
        cmsMobileGraphicNavigationVO.setModuleType(CmsUserModuleEnum.GraphicNavigation.getModuleType());
        cmsMobileGraphicNavigationVO.setOrderSort(cmsUserRedisIndexModule.getOrderSort());
        return cmsMobileGraphicNavigationVO;
    }

}
